package mobile.protobuf.demo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Message;

import common.msg.Options;
import common.msg.Test.TestMsg;

public class MainTest {

	public static int MessageToId(Message msg) {
		int id = msg.getDescriptorForType().getOptions().getExtension(Options.myOption);
		return id;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try {
			Descriptor dd = (Descriptor)(TestMsg.class.getMethod("getDescriptor").invoke(null));
			int ssString = dd.getOptions().getExtension(Options.myOption);
			System.out.println(ssString);
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		// 扩展字段读取
		common.msg.Test.TestMsg.getDefaultInstance();
		Descriptor descriptor1 = TestMsg.getDescriptor();
		int str = descriptor1.getOptions().getExtension(Options.myOption);
		System.out.println(str);
		
		Class<?> messageClass = common.msg.Test.TestMsg.class;
		
		try {
			Method method = messageClass.getMethod("getDescriptor");
			Descriptor dp = (Descriptor)(method.invoke(null));
			int string = dp.getOptions().getExtension(Options.myOption);
			System.out.println(string);
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		
		// 扩展字段读取
		int str1 = TestMsg.getDescriptor().getOptions().getExtension(Options.myOption);
		System.out.println(str1);
		
		TestMsg.Builder msgBuilder = TestMsg.newBuilder();
		msgBuilder.setId(102);
		msgBuilder.setName("jack");
		
		TestMsg msg = msgBuilder.build();
		
		
		int msgId = MessageToId(msg);
		
		System.out.println(msgId);
	}

}
